﻿using AutoMapper;
using MediatR;
using Provenance.Domain.Breed;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.Breed;
using Provenance.Read.Api.Application.Command.Breed;

namespace Provenance.Read.Api.Application.CommandHandler.Breed
{
    public class GetBreedHealthCommandHandler : IRequestHandler<GetBreedHealthCommand, ApiResult<IList<BreedHealth>>>
    {
        private readonly IBreedHealthRepository _breedHealthRepository;
        private readonly ILogger<GetBreedHealthCommandHandler> _logger;
        private readonly IMapper _mapper;
        public GetBreedHealthCommandHandler(IBreedHealthRepository breedingInfoRepository, ILogger<GetBreedHealthCommandHandler> logger, IMapper mapper)
        {
            _breedHealthRepository = breedingInfoRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<IList<BreedHealth>>> Handle(GetBreedHealthCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var list = await _breedHealthRepository.GetAllAsync(x => x.ProductInfoId == request.ProductInfoId&&!x.IsDeleted);
                return ApiResult<IList<BreedHealth>>.Success(list);
            }
            catch (Exception ex)
            {
                _logger.LogError("Error getting breed health" + ex.Message);
                return ApiResult<IList<BreedHealth>>.Failure(ex.Message);
            }
        }
    }
}
